<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :modal-append-to-body="false"
    :close-on-click-modal="false"
    width="960px"
  >
    <el-form
      label-position="right"
      label-width="80px"
      :model="form"
      :rules="rules"
      ref="menuFrom"
    >
      <!-- 菜单名称 图标 访问路径 路由名称 组件路径 -->
      <el-form-item
        label="菜单名称"
        prop="name"
      >
        <el-input
          v-model="form.name"
          placeholder="请输入菜单名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="菜单图标"
        prop="icon"
      >
        <el-input
          v-model="form.icon"
          placeholder="请输入菜单图标"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="访问路径"
        prop="path"
      >
        <el-input
          v-model="form.path"
          placeholder="请输入访问路径"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="路由名称"
        prop="routerName"
      >
        <el-input
          v-model="form.routerName"
          placeholder="请输入路由名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="组件路径"
        prop="component"
      >
        <el-input
          v-model="form.component"
          placeholder="请输入组件路径"
        >
          <template slot="prepend">@/views/</template>
        </el-input>
      </el-form-item>
      <el-form-item label="iframe">
        <el-input
          v-model="form.iframe"
          placeholder="请输入iframe链接"
        ></el-input>
      </el-form-item>
    </el-form>

    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="cancelFn('menuFrom')">取 消</el-button>
      <el-button
        type="primary"
        @click="sureFn('menuFrom')"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { createAuthority, updateAuthority } from '@/api/authority';

export default {
  name: 'ViewDialog',
  inject: ['parent'],
  props: {
    title: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    },
    appInfo: {
      type: Object,
      required: true
    },
    type: {
      type: Number,
      required: true
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set() {
        this.cancelFn('menuFrom');
      }
    }
  },
  data() {
    return {
      form: {
        name: '',
        icon: '',
        path: '',
        routerName: '',
        component: '',
        iframe: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入菜单名称',
            trigger: 'blur'
          }
        ],
        icon: [
          {
            required: true,
            message: '请输入菜单图标',
            trigger: 'blur'
          }
        ],
        path: [
          {
            required: true,
            message: '请输入页面访问路径',
            trigger: 'blur'
          }
        ],
        routerName: [
          {
            required: true,
            message: '请输入路由名称',
            trigger: 'blur'
          }
        ],
        component: [
          {
            required: true,
            message: '请输入组件路径',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  created() {
    if (this.type === 2) {
      // 修改
      let { name, icon, path, routerName, component, iframe } = this.appInfo;
      this.form = {
        name,
        icon,
        path,
        routerName,
        component,
        iframe
      };
    }
  },
  methods: {
    // 取消
    cancelFn(formName) {
      this.$refs[formName].resetFields();
      this.$emit('update:visible', false);
    },
    // 确认
    sureFn(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.submitFn();
        } else {
          return false;
        }
      });
    },
    // 提交
    submitFn() {
      let { name, icon, path, routerName, component, iframe } = this.form;
      let params = {
        name,
        icon,
        req_uri: path,
        router_name: routerName,
        component_uri: component,
        iframe
      };
      if (this.type === 1) {
        // 新增
        params.service_id = this.appInfo.id;
        params.service_name = this.appInfo.name;
        createAuthority(params).then(res => {
          this.operateFn(res);
        });
      } else {
        // 修改
        params.id = this.appInfo.id;
        params.service_id = this.appInfo.service_id;
        params.service_name = this.appInfo.service_name;
        updateAuthority(params).then(res => {
          this.operateFn(res);
        });
      }
    },
    // 提交操作
    operateFn(res) {
      this.$toast(res.message);
      this.cancelFn('menuFrom');
      this.$emit('refresh');
    }
  }
};
</script>

<style lang="scss" scoped>
</style>

